package com.gc.test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

public class ABATest {

	public static void main(String[] args) {
		final AtomicInteger ai=new AtomicInteger(100);
		final AtomicStampedReference<Integer> asf=new AtomicStampedReference<Integer>(100,1);
		Thread t1=new Thread(new Runnable(){

			@Override
			public void run() {
			ai.compareAndSet(100, 110);
			ai.compareAndSet(110, 100);
				
			}
			
			
			
		});
		Thread t2=new Thread(new Runnable(){

			@Override
			public void run() {
		        try {
					TimeUnit.MILLISECONDS.sleep(2);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}   
		       System.out.println(ai.compareAndSet(100, 120));
				
			}
			
		});
		t1.start();
		t2.start();
		try {
			t1.join();
			t2.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 Thread tf1=new Thread(new Runnable(){

		@Override
		public void run() {
			try {
				TimeUnit.MILLISECONDS.sleep(2);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		      asf.compareAndSet(100, 110, asf.getStamp(), asf.getStamp()+1);
		      asf.compareAndSet(110,100, asf.getStamp(),asf.getStamp()+1);
			
		}
		 	 
	 });
	 Thread tf2=new Thread(new Runnable(){

		@Override
		public void run() {
			   int stamp=asf.getStamp();
			   System.out.println("stamp=="+stamp);
			   try {
				TimeUnit.MILLISECONDS.sleep(2);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			   System.out.println(asf.compareAndSet(100, 120, stamp, asf.getStamp()+1));
			
		}
		 
	 });
	 tf1.start();
	 tf2.start();
	

	}

}
